package com.imay.live.activity.family;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.net.Uri;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
import com.imay.base.helper.ActivityHelper;
import com.imay.capturefilter.utils.ICUtils;
import com.imay.dialog.Dialoger;
import com.imay.live.R;
import com.imay.live.activity.BaseRequestActivity;
import com.imay.live.activity.family.controller.FamilyCreateController;
import com.imay.live.manager.AccountManager;
import com.imay.live.manager.FamilyAccountManager;
import com.imay.live.model.FamilyModel;
import com.imay.live.model.FamilyUserInfo;
import com.imay.live.model.SysparamModel;
import com.imay.live.proto.ErrorResult;
import com.imay.live.proto.Result;
import com.imay.live.proto.account.AccountCheckSmsResp;
import com.imay.live.utils.ToastUtil;
import com.yalantis.ucrop.UCrop;

import java.io.File;

import static com.imay.dialog.Dialoger.CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE;
import static com.imay.dialog.Dialoger.SYS_IMAGE_ACTIVITY_REQUEST_CODE;
import static com.imay.live.proto.ResultCode.CodeSuccess;
import static com.taobao.accs.ACCSManager.mContext;

/**
 * Created by Administrator on 2017/8/29 0029.
 */

public class FamilyCreateActivity extends BaseRequestActivity implements View.OnClickListener {

    private static final String STATUS_KEY = "status_key";

    private int status;

    public static final int CHECKING = 1;
    public static final int NOPASSING = 2;
    public static final int PASSING = 3;

    public static final int CREATE = 1;
    public static final int MODIFY = 2;
    public static final int REVIEW = 3;
    public static final int MODIFY_REVIEW = 4;

    private FamilyCreateController controler;

    private String createFamilyCommand, modifyFamilyCommand;

    /**
     * @param context
     */
    public static void startActivity(Context context, int status) {
        Intent intent = new Intent(context, FamilyCreateActivity.class);
        intent.putExtra(STATUS_KEY, status);
        context.startActivity(intent);
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_family_create;
    }

    @Override
    protected void initView(Bundle savedInstanceState) {
        status = getIntent().getIntExtra(STATUS_KEY, CREATE);
        FamilyUserInfo familyUserInfo = FamilyAccountManager.getInstance().getFamilyUserInfo();
        if (familyUserInfo != null) {
            FamilyModel familyBean = familyUserInfo.getFamily();
            if (familyBean != null && familyBean.getStatus() == CHECKING) {
                status = REVIEW;
            } else if (familyBean != null && familyBean.getEditStatus() == CHECKING) {
                status = MODIFY_REVIEW;
            }
        }
        controler = (FamilyCreateController) FamilyCreateController.Builder.create(this, FamilyCreateController.class).build();
        if (status == CREATE) {
            setTitle(R.string.str_family_create);
        } else if (status == MODIFY) {
            setTitle(R.string.str_family_modify);
            controler.hideName();
            controler.setData(familyUserInfo, true);
        } else if (status == REVIEW) {
            setTitle(R.string.str_family_create);
            controler.setData(familyUserInfo, false);
            controler.setButtom();
        } else if (status == MODIFY_REVIEW) {
            setTitle(R.string.str_family_modify);
            controler.hideName();
            controler.setEditData(familyUserInfo, false);
            controler.setButtom();
        }
    }

    @Override
    protected void onActivityResult(final int requestCode, final int resultCode, final Intent data) {
        switch (requestCode) {
            case SYS_IMAGE_ACTIVITY_REQUEST_CODE:
                if (resultCode == RESULT_OK) {
                    if (data != null) {
                        Uri uri = data.getData();
                        String path = Dialoger.getPath(FamilyCreateActivity.this, uri);
                        if (TextUtils.isEmpty(path)) {
                            ToastUtil.show(this, R.string.str_img_error);
                            return;
                        }
                        File file = ICUtils.getOutputMediaFile(FamilyCreateActivity.this);
                        UCrop.Options options = new UCrop.Options();
                        options.setToolbarColor(Color.BLACK);
                        options.setActiveWidgetColor(Color.BLACK);
                        options.setStatusBarColor(Color.BLACK);
                        options.setHideBottomControls(true);
                        UCrop.of(Uri.fromFile(new File(path)), Uri.fromFile(file))
                                .withOptions(options)
                                .withAspectRatio(9, 9)
                                .withMaxResultSize(300, 300)
                                .start(FamilyCreateActivity.this);
                    } else {
                        ToastUtil.show(mContext, R.string.str_img_error);
                    }
                }
                break;
            case CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE:
                if (RESULT_OK == resultCode) {
                    if (data != null) {
                        File file = ICUtils.getOutputMediaFile(FamilyCreateActivity.this);
                        String path = data.getStringExtra("path");
                        if (TextUtils.isEmpty(path)) {
                            ToastUtil.show(this, R.string.str_img_error);
                            return;
                        }
                        UCrop.Options options = new UCrop.Options();
                        options.setToolbarColor(Color.BLACK);
                        options.setActiveWidgetColor(Color.BLACK);
                        options.setStatusBarColor(Color.BLACK);
                        options.setHideBottomControls(true);
                        UCrop.of(Uri.fromFile(new File(path)), Uri.fromFile(file))
                                .withOptions(options)
                                .withAspectRatio(9, 9)
                                .withMaxResultSize(300, 300)
                                .start(FamilyCreateActivity.this);
                    } else {
                        ToastUtil.show(mContext, R.string.str_img_error);
                    }
                }
                break;
            case UCrop.REQUEST_CROP:
                if (RESULT_OK == resultCode) {
                    final Uri resultUri = UCrop.getOutput(data);
                    if (resultUri != null) {
                        String imagePath = Dialoger.getPath(FamilyCreateActivity.this, resultUri);
                        controler.upload(imagePath);
                    }
                }
                break;
            default:
                super.onActivityResult(requestCode, resultCode, data);
                break;
        }
    }

    @Override
    public void onRequestSuccess(String command, String data) {
        if (command.equals(createFamilyCommand)) {
            Result<FamilyModel> resp = JSONObject.parseObject(data, new TypeReference<Result<FamilyModel>>() {
            }.getType());
            if (resp.result == CodeSuccess) {
                ToastUtil.show(this, R.string.str_family_create_note);
                controler.setButtom();
            }
        } else if (command.equals(modifyFamilyCommand)) {
            Result<String> resp = JSONObject.parseObject(data, new TypeReference<Result<String>>() {
            }.getType());
            if (resp.result == CodeSuccess) {
                FamilyAccountManager.getInstance().loadFamilyUserInfo(false);
                ToastUtil.show(this, R.string.str_family_modify_note);
                controler.setButtom();
            }
        }
    }

    @Override
    public void onRequestError(String command, int code, String message) {
        ErrorResult.toastError(this, code);
        if (command.equals(createFamilyCommand)) {
            FamilyAccountManager.getInstance().clear();
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.sdv_family:
                Dialoger.showAcionSheetDialog(this);
                break;
            case R.id.tv_family_create:
                if (status == CREATE) {
                    createFamilyCommand = controler.createFamily();
                } else if (status == MODIFY) {
                    modifyFamilyCommand = controler.modifyFamily();
                }
                break;
        }
    }
}
